import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import EquipmentView from "@/views/EquipmentView.vue";
import ManageView from "@/views/ManageView.vue";
import EquipmentManageView from "@/views/manage/EquipmentManageView.vue";
import CultivationManageView from "@/views/manage/CultivationManageView.vue";
import InlayManageView from "@/views/manage/InlayManageView.vue";
import UserManageView from "@/views/manage/UserManageView.vue";
import EquipmentBuildsManageView from "@/views/manage/EquipmentBuildsManageView.vue";
import EquipmentBuildsHallView from "@/views/EquipmentBuildsHallView.vue";
import EquipmentBuildsDetailView from "@/views/EquipmentBuildsDetailView.vue";

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: HomeView,
        },
        {
            path: '/equipment',
            name: 'equipment',
            component: EquipmentView,
        },
        {
            path: '/equipment_build_hall',
            name: 'equipment_build_hall',
            component: EquipmentBuildsHallView,
        },
        {
            path: '/equipment_build_detail',
            name: 'equipment_build_detail',
            component: EquipmentBuildsDetailView,
        },
        {
            path: '/root/manage',
            name: 'root_manage',
            component: ManageView,
            beforeEnter: (to, from, next) => {
                // 从本地存储获取用户信息
                const user = JSON.parse(localStorage.getItem("user")) || {
                    id: 0,
                    username: '',
                    phone: null,
                    email: null,
                    permissionLevel: 1
                };
                if (user.permissionLevel === 3) {
                    // 用户权限级别为 3，允许访问
                    next();
                } else {
                    // 不满足条件，重定向到其他页面，比如首页
                    next('/');
                }
            },
            children: [
                {
                    path: 'equipment',
                    name: 'equipment_manage',
                    component: EquipmentManageView
                },
                {
                    path: 'equipment_builds',
                    name: 'equipment_builds_manage',
                    component: EquipmentBuildsManageView
                },
                {
                    path: 'cultivation',
                    name: 'cultivation_manage',
                    component: CultivationManageView
                },
                {
                    path: 'inlay',
                    name: 'inlay_manage',
                    component: InlayManageView
                },
                {
                    path: 'user',
                    name: 'user_manage',
                    component: UserManageView
                }
            ]
        }
    ],
})

export default router
